#define DEBUG damn skippy

#include <iostream>
#include <globals.h>

#include "player.cpp"
#include "stats.cpp"

using namespace std;

int main ( int argc, char ** argv ) {
	
#ifdef _WINDOWS
	// seed the random number gen
#else
	srand(time(NULL));	
#endif

	header(); // Online!

	player * p = new player();
	stats s;
	s.setName( "name" );
	s.setValue( "Fluff McJohnson" );
	p->addStat( &s ); // dummy data to handle boundry condition

	std::vector<player *> * roster = new std::vector<player * >();

	try {
		do {
			p = new player(load_data());
			roster->push_back( p );
			output_dbug( "Added new player" );
		} while ( p->runStat("name") );
	} catch ( attr_error& ) {
		output_dbug( "Planned failure. Sorry, C++." );
		output_dbug( "OK. I've got all the players. Starting to process." );
	}

	clean_list( roster );

	output_dbug("");
	output_dbug(" == ROSTER == ");
	output_dbug("");

	for ( unsigned int i = 0; i < roster->size(); ++i ) {
		output_msg( roster->at(i)->getStat("name")->getValue() );
	}

	output_dbug("");
	output_dbug(" == END ROSTER == ");
	output_dbug("");

	int hits  = 0;
	int fhits = 0;

	for ( int n = 0; n < 9 /*innings*/; ++n ) {
		for ( unsigned int i = 0; i < roster->size(); ++i ) {
			try {
				stats * obp      = roster->at(i)->getStat("obp");              // get the player's obp
				std::string name = roster->at(i)->getStat("name")->getValue(); // get the player's name
				if ( obp->processValue() ) {
					output_dbug("");
					output_msg( name );
					output_msg( "just got a hit" );
					++hits;
				} else {
					output_dbug("");
					output_msg( name );
					output_msg( "failed to get a hit" );
					++fhits;
				}
			} catch ( attr_error & ) {
				output_msg("Data Failure! We're missing an attr :(");
			}
		}
		output_dbug("end \"inning\"");
	}

	stringstream ss;
	ss << "Hits:   " << hits;
	std::string s_hits = ss.str();

	stringstream ss0;
	ss0 << "Fails:  " << fhits;
	std::string s_fhits = ss0.str();

	output_dbug("");
	output_dbug("END OF \"GAME\"!");
	output_dbug("");
	output_dbug("\"SCORE\"");
	output_dbug("");
	output_dbug( s_hits  );
	output_dbug( s_fhits );
	output_dbug("");

}
